home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / glass / glass.lha / GLASS / glammar / gg01.c < prev    next >
C/C++ Source or Header  |  1991-04-16  |  23KB  |  914 lines

  1. /*
  2.  
  3.     This file is a part of the GLAMMAR source distribution 
  4.     and therefore subjected to the copy notice below. 
  5.     
  6.     Copyright (C) 1989,1990  Eric Voss, ericv@cs.kun.nl 
  7.  
  8.     This program is free software; you can redistribute it and/or modify
  9.     it under the terms of the GNU General Public License as published by
  10.     the Free Software Foundation version 1
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. /* file    driver:  defines all global symbols, reads args, calls c-compiler */
  22. #include "gg1.h"
  23.  
  24. AST            *ast;
  25.  
  26. int   
  27.                 endofsentence, 
  28.                 nestaralt, 
  29.                 nestarset, 
  30.                 setinputptrto, 
  31.                 resetinputptr,
  32.                 getip, falseip, restoreip,
  33.                 shadow, unshadow,
  34.                 firstshadow, nextshadow,
  35.                 metaterminal,
  36.                 astindex,
  37.                 brother,
  38.                 root,
  39.                 startnode,
  40.                 underscore_allowed = true,
  41.                 lastnode,
  42.                 laststdpred,
  43.                 first_lattice = nil,
  44.                 symbol_table_size = 0,
  45.                 lastmetarule,
  46.                 laststdmetarule,
  47.                 charindex,
  48.                 hashindex,
  49.                 prevcharindex,
  50.                 usefullerrmsg,
  51.                 syntaxerrors,
  52.                 line,
  53.                 init_sum = 100,   /* current version number */
  54.                 sum =0,
  55.                 nrterms,
  56.                 nrntmems,
  57.                 lastsettype,
  58.                 lastaffixtree,
  59.                 defaffixtree,
  60.                 ulhs,
  61.                 reccount,
  62.                 RuleCount,
  63.                 NtCount,
  64.                 qCount,
  65.                 aCount,
  66.                 input_from_partlist,
  67.                 fulltrace_flag,
  68.                 afx_opt_flag,
  69.                 memopt_flag,
  70.                 AM_flag,
  71.                 lat_trad_flag,
  72.                 trace_flag,
  73.                 syntax_flag,
  74.                 errormsg_flag,
  75.                 stat_flag,
  76.                 fullstat_flag,
  77.                 memo_flag,
  78.                 nr_of_memo_alts,
  79.                 index_flag,
  80.                 hide_flag,
  81.                 input_from_stdin,
  82.                 lift_flag,
  83.                 meta_uniq_flag,
  84.                 det_flag,
  85.                 ambiguous_flag,
  86.                 separate_comp_flag,
  87.                 noerrmsg_flag,
  88.                 keep_flag,
  89.                 tree_flag,
  90.                 id_tree_flag,
  91.                 parse_tree_flag,
  92.                 hack_flag,
  93.                 cfg_flag,
  94.                 backtrace_mark,
  95.                 affix_trace,
  96.                 gnu_flag,
  97.                 link_flag,
  98.                 verbose_flag,
  99.                 eag_flag,
  100.                 name_clashes,
  101.                 optimize_flag,
  102.                 all_external_flag;
  103.  
  104. unsigned        max_item,maxchars = MAX_CHARS;
  105.  
  106.  
  107. long            runtime_stksize = 500000l;
  108. int            runtime_input_size;
  109.  
  110. char            thispart[256],
  111.                 outputfile[256],
  112.                 stddefs_g[256],
  113.                 thischar,
  114.                *string,
  115.                *ntname,
  116.                *dont_care = "dont_care",
  117.                **nametable,
  118.                *chartable,
  119.                 commandbuffer[1024],
  120.                 tempbuf[1024],
  121.                 c_flags[256],
  122.                 indexfilename[256],
  123.                 ex_filename[256],
  124.                 hack_dir[256],
  125.                 sep_comp_part[256],
  126.                *rulename,
  127.                *startname;
  128.  
  129. FILE           *output,
  130.                *input,
  131.                *partlist,
  132.                *indexfile;
  133.  
  134. int            nlcr,explintersect,intersect,
  135.                transformlattice,tltraditional,
  136.                transformlatticeterm,tltraditionalterm,
  137.                skip,cut,equal,init_one_star,
  138.                pair,unpair, notequal,where,initmint,initmeta,evalmeta;
  139. char            parts[2000];
  140. char            *partptr= parts;
  141. #ifdef ATARI
  142. const long      _stksize = 100000l;
  143. #endif
  144.  
  145. main(arg_count, arguments)
  146.   int             arg_count;
  147.   char          **arguments;
  148. {
  149.   int             input_file_set = false;
  150.   int             output_file_set = false;
  151.   char           *arg_zero = *arguments;
  152.   char           *last_letter = *arguments;
  153.  
  154.   AM_flag = true;
  155.   affix_trace = true;
  156.   all_external_flag = false;
  157.   ambiguous_flag = false;
  158.   backtrace_mark = 0;
  159.   cfg_flag = false;
  160.   det_flag = false;
  161.   eag_flag = false;
  162.   errormsg_flag = false;
  163.   fulltrace_flag = false;
  164.   gnu_flag = false;
  165.   hack_flag = false;
  166.   id_tree_flag = false;
  167.   index_flag = false;
  168.   { FILE *xxx = fopen (".glammar","r");
  169.     if (xxx == NULL)
  170.          hide_flag = false;
  171.     else {
  172.          hide_flag = true;
  173.          fclose (xxx);
  174.     }
  175.   }
  176.   input_from_partlist = false;
  177.   input_from_stdin = false;
  178.   keep_flag = false;
  179.   lift_flag = true;
  180.   link_flag = false;
  181.   memo_flag = false;
  182.   memopt_flag = false;
  183.   meta_uniq_flag = false;
  184.   noerrmsg_flag = false;
  185.   optimize_flag = false;
  186.   afx_opt_flag = false;
  187.   parse_tree_flag = false;
  188.   separate_comp_flag = false;
  189.   stat_flag = false;
  190.   syntax_flag = false;
  191.   trace_flag = false;
  192.   tree_flag = false;
  193.  
  194.   max_item = 3000;
  195.  
  196.   chartable = (char *) malloc(maxchars);
  197.   nametable = (char **) malloc(maxnt * 4);
  198.   if (arg_count == 1) {
  199.     fprintf(stderr, "Usage: glammar [ options ]  File | - \n");
  200.     exit(12);
  201.   }
  202.   init_sum = 0;
  203.   for (arguments += arg_count - 1; arg_zero != *arguments; arguments -= 1) {
  204.     switch (**arguments) {
  205.     case '-':
  206.       switch (*(++*arguments)) {
  207.       case '\0':
  208.         if (output_file_set && !input_file_set) {
  209.           input_from_stdin = true;
  210.           input_file_set = true;
  211.         } else if (!output_file_set) {
  212.           output = stdout;
  213.           output_file_set = true;
  214.         } else {
  215.           fprintf(stderr, "glammar: `-' ignored\n");
  216.         }
  217.         break;
  218.       case 'L':
  219.         lat_trad_flag = true;
  220.         init_sum +=  (int) 'L';
  221.         break;
  222.       case 'l':
  223.         sscanf(*arguments + 1, "%ld", &runtime_stksize);
  224.         runtime_stksize = 100000l * runtime_stksize;
  225.         break;
  226.       case 'A':
  227.         afx_opt_flag = true;
  228.         break;
  229.       case 'a':
  230.         init_sum +=  (int) 'a';
  231.         ambiguous_flag = true;
  232.         break;
  233.       case 'C':
  234.       case 'c':
  235.         hack_flag = true;
  236.         (void) strcpy(hack_dir, *arguments + 1);
  237.         break;
  238.       case 'E':
  239.         init_sum +=  (int) 'E';
  240.         errormsg_flag = true;
  241.         det_flag = true;
  242.         break;
  243.       case 'e':
  244.         eag_flag = true;
  245.         break;
  246.       case 'F':
  247.       case 'f':
  248.         lift_flag = false;
  249.         init_sum +=  (int) 'F';
  250.         break;
  251.       case 'd':
  252.         det_flag = true;
  253.         break;
  254.  
  255.       case 'G':
  256.       case 'g':
  257.         gnu_flag = true;
  258.         break;
  259.       case 'H':
  260.          hide_flag = true;
  261.          break;
  262.       case 'h':
  263.         exit(system("man glammar"));
  264.       case 'I':
  265.         init_sum +=  (int) 'i';
  266.         fullstat_flag = true;
  267.       case 'i':
  268.         stat_flag = true;
  269.         init_sum +=  (int) 'i';
  270.         break;
  271.       case 'K':
  272.       case 'k':
  273.         keep_flag = true;
  274.         break;
  275.       case 'P':
  276.         all_external_flag = true;
  277.       case 'p':
  278.         separate_comp_flag = true;
  279.         input_from_partlist = true;
  280.         input_file_set = true;
  281.         break;
  282.       case 'X':
  283.         syntax_flag = true;
  284.       case 'x':
  285.         index_flag = true;
  286.         break;
  287.       case 'R':
  288.       case 'r':
  289.         switch (*(*arguments +1)) {
  290.           case '\0': tree_flag = true; break;
  291.           case 'i': id_tree_flag = true; break;
  292.           case 'p': parse_tree_flag = true; break;
  293.           case 'c': cfg_flag = true; break;
  294.           default:;
  295.         }
  296.         tree_flag = true;
  297.         break;
  298.       case 'O':
  299.         (void) sprintf(tempbuf, " -%c %s ", *(*arguments + 1), *arguments + 2);
  300.         (void) strcat(c_flags, tempbuf);
  301.         break;
  302.       case 'o':
  303.         (void) sprintf(tempbuf, " -%s ", *arguments + 1);
  304.         (void) strcat(c_flags, tempbuf);
  305.         init_sum +=  9;
  306.         break;
  307.       case 'S': 
  308.       case 's':
  309.         syntax_flag = true;
  310.         break;
  311.       case 'T':
  312.         fulltrace_flag = true;
  313.         init_sum +=  (int) 't';
  314.       case 't':
  315.         init_sum +=  (int) 't';
  316.         trace_flag = true;
  317.         break;
  318.       case 'V':
  319.         verbose_flag = true;
  320.         break;
  321.       case 'v':
  322. #ifndef VERSION
  323. #define VERSION "1.00 Nov 7 1990"
  324. #endif
  325.        fprintf(stderr,"Glammar (c) %s\n",VERSION);
  326.       case 'U':
  327.         meta_uniq_flag = true;
  328.         break;
  329.       case 'M':
  330.          AM_flag = false;  /* No advance memoizing */
  331.       case 'm':
  332.         memo_flag = true;
  333.         sscanf(*arguments + 1, "%d", &runtime_input_size);
  334.         if (runtime_input_size < 1)
  335.            runtime_input_size = 2;
  336.         runtime_input_size <<= 10;
  337.         break;
  338.       default:
  339.          fprintf(stderr, "glammar: Unrecognized option `-%c'\n   \
  340. If you want to pass this option to \"cc\" type: glammar -o%s\n",
  341.           **arguments, *arguments);
  342.       }
  343.       break;
  344.     default:
  345.       {
  346.         char           *fname,
  347.                        *arg = *arguments;
  348.  
  349.         if (output_file_set && !input_file_set) {
  350.           fname = thispart;
  351.             while (*arg != '\0')
  352.               *fname++ = *arg++;
  353.           *fname++ = '.';
  354.           last_letter = fname++;
  355.           *fname++ = '\0';
  356.           *last_letter = 'g';
  357.           input = fopen(thispart, "r");
  358.           if (input == NULL)  {
  359.              *last_letter =  'p';
  360.              input = fopen(thispart, "r");
  361.              if (input != NULL)  {
  362.               fclose (input);
  363.               input_from_partlist = true;
  364.               separate_comp_flag = true;
  365.              }
  366.               else {
  367.                fprintf(stderr, "glammar: %s: No such file\n", thispart);
  368.                  exit(12);
  369.               }
  370.           }
  371.           input_file_set = true;
  372.           *--last_letter = '\0';
  373.         } else if (!output_file_set) {
  374.           fname = thispart;
  375.  
  376.           while (*arg != '\0')
  377.             *fname++ = *arg++;
  378.  
  379.           *fname++ = '.';
  380.           last_letter = fname++;
  381.           *fname++ = '\0';
  382.           *--last_letter = '_';
  383.           *++last_letter = '.';
  384.           *++last_letter = 'c';
  385.           *++last_letter = '\0';
  386.           output_file_set = true;
  387.           (void) strcpy(outputfile, thispart);
  388.           last_letter -= 4;
  389.           *++last_letter = '.';
  390.           *++last_letter = 'g';
  391.           *++last_letter = '\0';
  392.           input = fopen(thispart, "r");
  393.           if (input == NULL)  {
  394.              *(last_letter-1) =  'p';
  395.              input = fopen(thispart, "r");
  396.              if (input != NULL)  {
  397.               fclose (input);
  398.               input_from_partlist = true;
  399.               separate_comp_flag = true;
  400.              }
  401.              else *(last_letter-1) =  'g';
  402.           }
  403.           else    fclose (input);
  404.           last_letter -= 3;
  405.         } else {
  406.           fprintf(stderr, "glammar: argument `%s' ignored\n");
  407.         }
  408.         break;
  409.       }
  410.     }
  411.   }
  412.   if (!output_file_set || (input_file_set && !input_from_partlist) ){
  413.     fprintf(stderr, "Usage: glammar [ options ] File | -\n");
  414.     exit(12);
  415.   }
  416.   if (!input_file_set) {
  417.     if (output == stdout) {
  418.       input = stdin;
  419.       input_from_stdin = true;
  420.     } else
  421.       input = fopen(thispart, "r");
  422.  
  423.     if (input == NULL) {
  424.             fprintf(stderr, "glammar: %s: No such file or directory\n",
  425.         thispart);
  426.       exit(12);
  427.     }
  428.     input_file_set = true;
  429.   }
  430.   if (output != stdout) {
  431.     *++last_letter = '\0';
  432.     (void) sprintf(ex_filename, "%s\0", thispart);
  433.   }
  434.   if ((index_flag) && (output == stdout))
  435.     indexfile = stdout;
  436.   else if (index_flag) {
  437.     (void) sprintf(indexfilename, "%s.x", thispart);
  438.     indexfile = fopen(indexfilename, "w");
  439.      if (indexfile == NULL) {
  440.        fprintf(stderr, "Glammar fatal msg: cannot write to %s\n",
  441.          indexfilename);
  442.        exit(1);
  443.      }
  444.   }
  445.   /* do first pass */
  446.  
  447.   ast = (AST *) malloc((max_item + 2) * 20);
  448.  
  449.   if (ast == NULL) {
  450.     fprintf(stderr, "Glammar fatal msg: not enough heap space available\n");
  451.     exit(10);
  452.   }
  453.   if (verbose_flag)
  454.     fprintf(stderr, "Arguments read and Storage allocated.\n");
  455.   if (separate_comp_flag && !link_flag)  {
  456.    char *y = sep_comp_part, *x = outputfile;
  457.           if (hide_flag) {
  458.            (void) strcpy(outputfile, ".glammar/");
  459.            x += 9;
  460.           }
  461.    for (; *y != '\0' ; y++) 
  462.      *x++ = *y;
  463.    *x++  = '.';
  464.    *x++  = 'c';
  465.    *x  = '\0';
  466.   }
  467.   compile();
  468.   free(ast);
  469.   free(nametable);
  470.   free(chartable);
  471.   if (syntax_flag)
  472.     exit(syntaxerrors);
  473.   /* do C-assembler pass */
  474.   if (output == stdout)
  475.     exit(0);
  476.   if (verbose_flag)
  477.     fprintf(stderr, "C-compiler phase\n");
  478.  
  479.     (void) strcat(commandbuffer, CC);
  480.  
  481.     (void) strcat(commandbuffer, CCFLAGS);
  482.  
  483.   if (separate_comp_flag && !link_flag)  
  484.      (void) strcat(commandbuffer, "-c ");
  485.   else {
  486.      (void) strcat(commandbuffer, "-o ");
  487.      (void) strcat(commandbuffer, ex_filename);
  488.   }
  489.   (void) strcat(commandbuffer, c_flags);
  490.  
  491. #ifdef WARN
  492.   (void) strcat(commandbuffer, " -w ");
  493. #else
  494.   (void) strcat(commandbuffer, " ");
  495. #endif
  496.  
  497. #ifdef DEC_MIPS
  498.   (void) strcat(commandbuffer, " -G 0 ");
  499. #else
  500.   (void) strcat(commandbuffer, " ");
  501. #endif
  502.   if (keep_flag)
  503.     (void) strcat(commandbuffer, " -temp=. ");
  504.  
  505.    if (!link_flag) {
  506.    (void) strcat(commandbuffer, outputfile);
  507.    (void) strcat(commandbuffer, " ");
  508.    }
  509.    else  
  510.      (void) strcat(commandbuffer, parts);
  511.    
  512.    if (!separate_comp_flag || link_flag) {
  513.      if (hack_flag) {
  514.       (void) strcat(commandbuffer, hack_dir);
  515.        (void) strcat(commandbuffer, "/glext.o");
  516.      }
  517.      (void) strcat(commandbuffer, " ");
  518.      (void) strcat(commandbuffer, LIBDIR); 
  519.      (void) strcat(commandbuffer, "/glammar.a ");
  520. #ifndef NOMP
  521. #ifndef MP
  522.      (void) strcat(commandbuffer, "-lmp ");
  523. #else MP
  524.      (void) strcat(commandbuffer, MP);
  525. #endif MP
  526. #endif NOMP
  527.    }
  528.   
  529. /*  if (verbose_flag) */
  530.       fprintf(stderr, "%s\n", commandbuffer);
  531.   if (system(commandbuffer) > 0) {
  532.     fprintf(stderr,
  533.       "Glammar fatal msg: (C) assembler phase failed.\n"); exit(1);
  534.    }
  535. #ifndef NOSTRIP
  536.     (void) sprintf(commandbuffer, "strip %s", ex_filename);
  537.    if (verbose_flag) 
  538.       fprintf(stderr, "%s\n", commandbuffer);
  539.     if (system(commandbuffer) > 0) {
  540.        fprintf(stderr,
  541.         "Glammar fatal msg: %s: strip failed\n",ex_filename); exit(1);
  542.     }
  543. #endif
  544.  
  545. #ifdef RMOBJECT
  546.     (void) sprintf(commandbuffer, "rm -f %s %s_.o", outputfile, ex_filename);
  547. #else
  548.     (void) sprintf(commandbuffer, "rm -f %s ", outputfile);
  549. #endif
  550.  
  551.     if (verbose_flag)
  552.       fprintf(stderr, "%s\n", commandbuffer);
  553.     exit(system(commandbuffer));
  554. }
  555.  
  556.  
  557. glm_options()
  558. {
  559.    while ((thischar == '\t' || thischar == ' ' || thischar ==
  560.            '\n') &&
  561.           (!feof(input))) {
  562.       if (thischar == '\n')
  563.          line += 1;
  564.       thischar = getc(input);
  565.    }
  566.  
  567.    if (thischar == '#') {
  568.          thischar = getc(input);
  569.          if (thischar != '-') {
  570.                  if (thischar != '\n') 
  571.                     thischar = '#';
  572.                  skiplayout();
  573.                  return;
  574.          }
  575.         
  576.          read_glm_option();
  577.          thischar = '\n';
  578.          glm_options();
  579.    }
  580.    if (thischar == '%') {
  581.       do {
  582.          thischar = getc(input);
  583.          if (thischar == '\n')
  584.             line += 1;
  585.       } while ((thischar != '%') && (!feof(input)));
  586.  
  587.       getfirstchar();
  588.    }
  589. }
  590.  
  591. read_glm_option()
  592. {
  593.  
  594.     char  arg[256],skip[256];
  595.  
  596.     char *args = arg;
  597.     fscanf (input, "%s",args);
  598.     fgets (skip,256,input);
  599.         switch ( *args) {
  600.         case '-':
  601.         case ' ':
  602.         case '\t':
  603.             break;
  604.         case 'L':
  605.                         init_sum +=  (int) 'L';
  606.             lat_trad_flag = true;
  607.             break;
  608.         case 'l':
  609.             sscanf(args + 1, "%ld", &runtime_stksize);
  610.             runtime_stksize = 100000l * runtime_stksize;
  611.             break;
  612.         case 'A':
  613.             afx_opt_flag = true;
  614.             break;
  615.         case 'a':
  616.                         init_sum +=  (int) 'a';
  617.             ambiguous_flag = true;
  618.             break;
  619.         case 'C':
  620.         case 'c':
  621.             hack_flag = true;
  622.             (void) strcpy(hack_dir, args + 1);
  623.             break;
  624.         case 'E':
  625.                         init_sum +=  (int) 'E';
  626.             errormsg_flag = true;
  627.             det_flag = true;
  628.             break;
  629.         case 'e':
  630.             eag_flag = true;
  631.             break;
  632.         case 'F':
  633.         case 'f':
  634.             lift_flag = false;
  635.             break;
  636.         case 'd':
  637.             det_flag = true;
  638.             break;
  639.  
  640.         case 'G':
  641.         case 'g':
  642.             gnu_flag = true;
  643.             break;
  644.  
  645.                 case 'H':
  646.                    hide_flag = true;
  647.                    break;
  648.         case 'h':
  649.             exit(system("man glammar"));
  650.         case 'I':
  651.             fullstat_flag = true;
  652.                         init_sum +=  (int) 'i';
  653.         case 'i':
  654.             stat_flag = true;
  655.                         init_sum +=  (int) 'i';
  656.             break;
  657.         case 'K':
  658.         case 'k':
  659.             keep_flag = true;
  660.             break;
  661.         case 'P':
  662.             all_external_flag = true;
  663.         case 'p':
  664.             separate_comp_flag = true;
  665.             input_from_partlist = true;
  666.             break;
  667.         case 'X':
  668.             syntax_flag = true;
  669.         case 'x':
  670.             index_flag = true;
  671.             break;
  672.         case 'R':
  673.         case 'r':
  674.             switch (args[1]) {
  675.             case '\0': 
  676.                 tree_flag = true; 
  677.                 break;
  678.             case 'i': 
  679.                 id_tree_flag = true; 
  680.                 break;
  681.             case 'p': 
  682.                 parse_tree_flag = true; 
  683.                 break;
  684.             case 'c': 
  685.                 cfg_flag = true; 
  686.                 break;
  687.             default:
  688.                 ;
  689.             }
  690.             tree_flag = true;
  691.             break;
  692.         case 'O':
  693.             (void) sprintf(tempbuf, " -%c %s ", args[1], args + 2);
  694.             (void) strcat(c_flags, tempbuf);
  695.             break;
  696.         case 'o':
  697.             (void) sprintf(tempbuf, " -%s ", args + 1);
  698.             (void) strcat(c_flags, tempbuf);
  699.             break;
  700.         case 'S':
  701.         case 's':
  702.             syntax_flag = true;
  703.             break;
  704.         case 'T':
  705.             fulltrace_flag = true;
  706.                         init_sum +=  (int) 't';
  707.         case 't':
  708.             trace_flag = true;
  709.                         init_sum +=  (int) 't';
  710.             break;
  711.         case 'V':
  712.             verbose_flag = true;
  713.             break;
  714.         case 'v':
  715. #ifndef VERSION
  716. #define VERSION "1.00 Sep 22 1990"
  717. #endif
  718.             fprintf(stderr,"Glammar (c) %s\n",VERSION);
  719.         case 'U':
  720.             meta_uniq_flag = true;
  721.             break;
  722.         case 'M':
  723.             AM_flag = false;  /* No advance memoizing */
  724.         case 'm':
  725.             memo_flag = true;
  726.             sscanf(args + 1, "%d", &runtime_input_size);
  727.             if (runtime_input_size < 1)
  728.                 runtime_input_size = 2;
  729.             runtime_input_size <<= 10;
  730.             break;
  731.         default: 
  732.             break;
  733.         }
  734. }
  735.   
  736.  
  737. code_includes() {
  738.   if (separate_comp_flag && !MARKED(root,docompile))
  739.      fprintf(output, "#include \"%s/glammar3.h\"\n", INCLUDEDIR);
  740.   else
  741.      fprintf(output, "#include \"%s/glammar2.h\"\n", INCLUDEDIR);
  742. }
  743.  
  744. /* file : separate compilation
  745.            generate code and compile for each module  with
  746.                       different sum then in the one in the partlist
  747. */
  748.  
  749.  
  750. compile_parts()
  751. {
  752.    int             cc = root,
  753.                    rule = root,
  754.                    up_to_date = true;
  755.    for (cc = root; cc > laststdpred; cc = newcc(cc)) {
  756.       sum = 0;
  757.       for (rule = root; rule != laststdpred; rule = BROTHER(rule))
  758.      if (PART(cc) == PART(rule)) {
  759.         SET(rule, docompile);
  760.             sum += SUM(rule);
  761.          }  
  762.      else
  763.         UNSET(rule, docompile);
  764.        NEWSUM(cc) = sum;
  765.       if (sum != OLDSUM(cc) || no_object(PART(cc))) {
  766.         char           *y = (char *) PART(cc),
  767.                        *x = outputfile;
  768.  
  769.           if (hide_flag) {
  770.            (void) strcpy(outputfile, ".glammar/");
  771.            x += 9;
  772.           }
  773.         for (; *y != '\0'; y++)
  774.            *x++ = *y;
  775.         *x++ = '.';
  776.         *x++ = 'c';
  777.         *x = '\0';
  778.      code();
  779.          up_to_date = false;
  780.      c_compiler();
  781.          *commandbuffer = '\0';
  782.       }
  783.    }
  784.    if (up_to_date && have_prog())  {
  785.             fprintf (stderr, "glammar: `%s' is up to date.\n", ex_filename);
  786.             exit(0);
  787.    } 
  788.    write_part_file();
  789.    link_flag = true;
  790. }
  791. extern char *part_file;
  792. c_compiler()
  793. {
  794.  
  795.    /* do C-assembler pass */
  796.    if (output == stdout)
  797.       return;
  798.    if (verbose_flag)
  799.       fprintf(stderr, "C-compiler phase\n");
  800. #ifdef CC
  801.       (void) strcat(commandbuffer, CC);
  802. #else
  803.       (void) strcat(commandbuffer, "cc ");
  804. #endif
  805. #ifdef CCFLAGS
  806.       (void) strcat(commandbuffer, CCFLAGS);
  807. #endif
  808.    if (separate_comp_flag && !link_flag) {
  809.       (void) strcat(commandbuffer, "-c ");
  810.       if (hide_flag) {
  811.           char ofile[256],*p;
  812.           (void) strcpy(ofile,outputfile);
  813.           for (p= ofile; *p != '\0'; p++);
  814.           *(p-1) = 'o';
  815.           *p++ = ' ';
  816.           *p = '\0';
  817. #ifndef IBM6000
  818.           (void) strcat(commandbuffer, "-o ");
  819.           (void) strcat(commandbuffer, ofile);
  820. #endif
  821.       }
  822.         
  823.    } else {
  824.       (void) strcat(commandbuffer, "-o ");
  825.       (void) strcat(commandbuffer, ex_filename);
  826.    }
  827.    (void) strcat(commandbuffer, c_flags);
  828.    if (!link_flag) {
  829.       (void) strcat(commandbuffer, outputfile);
  830.       (void) strcat(commandbuffer, " ");
  831.    } else
  832.       (void) strcat(commandbuffer, parts);
  833.  
  834. /*  if (verbose_flag) */
  835.    fprintf(stderr, "%s\n", commandbuffer);
  836.    if (system(commandbuffer) > 0)  {
  837.       fprintf(stderr,
  838.           "Glammar fatal msg: (C) assembler phase failed.\n");
  839.       exit(1);
  840.    }
  841.    (void) sprintf(commandbuffer, "/bin/rm -f %s",outputfile);
  842.    system(commandbuffer);
  843.    *commandbuffer = '\0';
  844. #ifdef IBM6000
  845.    if (separate_comp_flag && !link_flag && hide_flag) {
  846.           char ofile[256],*p;
  847.           (void) strcpy(ofile,outputfile);
  848.           for (p= ofile; *p != '\0'; p++);
  849.           *(p-1) = 'o';
  850.           *p++ = ' ';
  851.           *p = '\0';
  852.           (void) sprintf(commandbuffer, "/bin/mv  %s %s",ofile+9,ofile);
  853.           system(commandbuffer);
  854.           *commandbuffer = '\0'; 
  855.    }
  856. #endif IBM6000
  857. }
  858.  
  859. int have_prog()
  860. {
  861.    FILE           *f;
  862.    f = fopen(ex_filename, "r");
  863.    if (f == NULL)
  864.       return false;
  865.    fclose(f);
  866.    return true;
  867. }
  868.  
  869. no_object(repr)
  870.    char           *repr;
  871. {
  872.    char            obj[255];
  873.    FILE           *f;
  874.    if (hide_flag)
  875.         sprintf(obj, ".glammar/%s.o", repr);
  876.    else sprintf(obj, "%s.o", repr);
  877.    f = fopen(obj, "r");
  878.    if (f == NULL)
  879.       return true;
  880.    fclose(f);
  881.    return false;
  882. }
  883.  
  884. int 
  885. newcc(cc)
  886.    int             cc;
  887. {
  888.    int             rule;
  889.    int             part_cc = PART(cc);
  890.    for (rule = cc; rule != init_one_star; rule = BROTHER(rule))
  891.       if (PART(rule) != part_cc)
  892.      return rule;
  893.    return 0;
  894. }
  895.  
  896. write_part_file()
  897. {
  898.    int             rule,
  899.                    prev = root;
  900.    fclose(partlist);
  901.    partlist = fopen(part_file, "w");
  902.    fprintf(partlist, "%s %d\n", PART(root), NEWSUM(root));
  903.    for (rule = root; rule !=  init_one_star; prev = rule, rule = BROTHER(rule))
  904.       if (PART(rule) != PART(prev))
  905.      fprintf(partlist, "%s %d\n", PART(rule), NEWSUM(rule));
  906.    fclose(partlist);
  907. }
  908.  
  909. get_stddefs()
  910. {
  911.    (void) sprintf(stddefs_g, "%s/glammar.g", LIBDIR);
  912. }
  913.  
  914.